home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 23 web forms and controls / databinding / dynamictemplateform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-17  |  6.9 KB  |  173 lines

  1. Imports System.Data.OleDb
  2.  
  3. Public Class DynamicTemplateForm
  4.     Inherits System.Web.UI.Page
  5.     Protected WithEvents ddlItemTemplate As System.Web.UI.WebControls.DropDownList
  6.     Protected WithEvents ddlAlternateTemplate As System.Web.UI.WebControls.DropDownList
  7.     Protected WithEvents btnCustom As System.Web.UI.WebControls.Button
  8.     Protected WithEvents DataList1 As System.Web.UI.WebControls.DataList
  9.  
  10. #Region " Web Form Designer Generated Code "
  11.  
  12.     'This call is required by the Web Form Designer.
  13.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  14.  
  15.     End Sub
  16.  
  17.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  18.         'CODEGEN: This method call is required by the Web Form Designer
  19.         'Do not modify it using the code editor.
  20.         InitializeComponent()
  21.     End Sub
  22.  
  23. #End Region
  24.  
  25.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  26.         ' Load template names in the two DropDownList controls
  27.         If Not Me.IsPostBack Then
  28.             ' Load template names in a hashtable.
  29.             Dim ht As New Hashtable(4)
  30.             ht.Add("Black on yellow", "Black_on_yellow.ascx")
  31.             ht.Add("Blue on orange", "Blue_on_orange.ascx")
  32.             ht.Add("Blue on white", "Blue_on_white.ascx")
  33.             ht.Add("White on blue", "White_on_blue.ascx")
  34.  
  35.             ' Load these values into the two DropDownList controls
  36.             ddlItemTemplate.DataSource = ht
  37.             ddlItemTemplate.DataTextField = "Key"
  38.             ddlItemTemplate.DataValueField = "Value"
  39.             ddlItemTemplate.DataBind()
  40.  
  41.             ddlAlternateTemplate.DataSource = ht
  42.             ddlAlternateTemplate.DataTextField = "Key"
  43.             ddlAlternateTemplate.DataValueField = "Value"
  44.             ddlAlternateTemplate.DataBind()
  45.  
  46.             ' ensure no item is selected
  47.             ddlItemTemplate.SelectedIndex = -1
  48.             ddlAlternateTemplate.SelectedIndex = -1
  49.  
  50.             BindDataList()
  51.         End If
  52.     End Sub
  53.  
  54.     ' Display some titles from the Pubs database
  55.  
  56.     Sub BindDataList()
  57.         Dim cn As New OleDbConnection(OledbPubsConnString)
  58.         cn.Open()
  59.         Dim cmd As New OleDbCommand("SELECT * FROM Titles", cn)
  60.         Dim dr As OleDbDataReader = cmd.ExecuteReader
  61.         DataList1.DataSource = dr
  62.         DataList1.DataBind()
  63.         dr.Close()
  64.         cn.Close()
  65.     End Sub
  66.  
  67.     ' Load a new .ascx from file
  68.  
  69.     Private Sub ddlItemTemplate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlItemTemplate.SelectedIndexChanged, ddlAlternateTemplate.SelectedIndexChanged
  70.         ' Get the name of the template file.
  71.         Dim file As String = ddlItemTemplate.SelectedItem.Value
  72.         ' Load the template and rebind the DataList control.
  73.         DataList1.ItemTemplate = Me.LoadTemplate(file)
  74.  
  75.         ' Get the name of the alternate template file.
  76.         file = ddlAlternateTemplate.SelectedItem.Value()
  77.         ' Load the template and rebind the DataList control.
  78.         DataList1.AlternatingItemTemplate = Me.LoadTemplate(file)
  79.         BindDataList()
  80.     End Sub
  81.  
  82.     ' Display the custom template
  83.  
  84.     Private Sub btnCustom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustom.Click
  85.         DataList1.AlternatingItemTemplate = Nothing
  86.         DataList1.ItemTemplate = New MyCustomTemplate(Color.Blue, Color.White, Color.Cyan, Color.LightGreen)
  87.         BindDataList()
  88.     End Sub
  89. End Class
  90.  
  91. ' this is the custom template class
  92.  
  93. Class MyCustomTemplate
  94.     Implements ITemplate
  95.  
  96.     Public ForeColor As Color
  97.     Public BackColor As Color
  98.     Public AltBackColor As Color
  99.     Public AltBackColor2 As Color
  100.  
  101.     ' a suitable constructor
  102.     Sub New(ByVal foreColor As Color, ByVal backColor As Color, ByVal altBackColor As Color, ByVal altBackColor2 As Color)
  103.         Me.ForeColor = foreColor
  104.         Me.BackColor = backColor
  105.         Me.AltBackColor = altBackColor
  106.         Me.AltBackColor2 = altBackColor2
  107.     End Sub
  108.  
  109.     ' We must trap the DataBinding event for these controls.
  110.     Dim WithEvents pan As Panel
  111.  
  112.     Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
  113.         ' Get a strongly-typed reference to the containing item
  114.         Dim dli As DataListItem = DirectCast(container, DataListItem)
  115.  
  116.         ' Create a panel with specified colors.
  117.         pan = New Panel()
  118.         pan.ForeColor = Me.ForeColor
  119.         ' Use alternate colors for background.
  120.         Select Case dli.ItemIndex Mod 3
  121.             Case 0 : pan.BackColor = Me.BackColor
  122.             Case 1 : pan.BackColor = Me.AltBackColor
  123.             Case 2 : pan.BackColor = Me.AltBackColor2
  124.         End Select
  125.         ' Adapt to the container's size.
  126.         pan.Width = dli.Width
  127.         pan.Height = dli.Height
  128.  
  129.         ' Add controls to this panel.
  130.         pan.Controls.Add(New LiteralControl("<b>"))
  131.         pan.Controls.Add(New Label())                         ' this is the title field
  132.         pan.Controls.Add(New LiteralControl("</b> - "))
  133.         pan.Controls.Add(New Label())                         ' this is the price field
  134.  
  135.         ' Add the Panel control to the DataListItem control.
  136.         dli.Controls.Add(pan)
  137.     End Sub
  138.  
  139.     ' this event fires once for each item in the DataList, as it
  140.     ' is being bound to the data source
  141.  
  142.     Private Sub pan_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles pan.DataBinding
  143.         ' Get a reference to the item container.
  144.         ' (Note: cast to a DataGridItem if working with a DataGrid.)
  145.         Dim dli As DataListItem = DirectCast(pan.NamingContainer, DataListItem)
  146.         ' Get a reference to the data source row.
  147.         ' (Note: cast to a DataRowView if binding to a DataTable/DataView.
  148.         Dim dbr As System.Data.Common.DbDataRecord = DirectCast(dli.DataItem, System.Data.Common.DbDataRecord)
  149.  
  150.         ' Get the values of all the fields you're interested in
  151.         ' and display them in the Panel child controls.
  152.         Dim lblTitle As Label = DirectCast(pan.Controls(1), Label)
  153.         lblTitle.Text = dbr("title").ToString
  154.         ' This is the price Label control.
  155.         Dim lblPrice As Label = DirectCast(pan.Controls(3), Label)
  156.  
  157.         If dbr("price").ToString.Length > 0 Then
  158.             ' Display price if this field isn't Null.
  159.             lblPrice.Text = "$" & dbr("price").ToString
  160.             ' use special attributes for pricey titles.
  161.             If CDec(dbr("price")) > 20 Then
  162.                 lblPrice.ForeColor = Color.Red
  163.                 lblPrice.Font.Bold = True
  164.             End If
  165.         Else
  166.             ' Display special message for Null values
  167.             lblPrice.Text = "(unknown price)"
  168.             lblPrice.Font.Italic = True
  169.         End If
  170.  
  171.     End Sub
  172. End Class
  173.